package com.shop2cn.iapi.sqidcard.testcase;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqidcard.parameter.SaveIdcardReq;
import com.shop2cn.iapi.sqidcard.parameter.UpdatePushStatusReq;
import com.shop2cn.iapi.sqidcard.service.SaveIdcardCall;
import com.shop2cn.iapi.sqidcard.service.UpdatePushStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqidcard.IdcardInfo;
import com.ymttest.database.sqlwapper.sqidcard.IdcardInfoWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_UpdatePushStatus {
	
	//java接口
	private static UpdatePushStatusReq updatePushStatusReq;
	private static UpdatePushStatusCall updatePushStatusCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新推送状态");
	}

	@Before
	public void caseUp() {
		updatePushStatusReq = new UpdatePushStatusReq();
		updatePushStatusCall = new UpdatePushStatusCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updatePushStatusCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		IdcardInfoWrapper idcardInfoWrapper = new IdcardInfoWrapper();

		IdcardInfo expectResult = idcardInfoWrapper
				.selectByIdcardIdAndUserId(updatePushStatusReq.getIdcardId(), updatePushStatusReq.getUserId());

		Logger.verifyEquals(expectResult.getPush_status(),
				updatePushStatusReq.getPushStatus(), "验证Push_status");
		
	}
	
	public HashMap<String, Long> addIdcard(Boolean pushStatus) throws ClientProtocolException, IOException {

		SaveIdcardReq saveIdcardReq = new SaveIdcardReq();
		SaveIdcardCall saveIdcardCall = new SaveIdcardCall();

		saveIdcardReq.setIdcardNo(TestDataManager.getCardId(18, 3));
		saveIdcardReq.setIdcardName(TestDataManager.getChineseName());
		saveIdcardReq.setUserId(TestDataManager.getRandomIntNum(8) * 10000L);
		saveIdcardReq.setPushStatus(pushStatus);

		saveIdcardCall.setData(saveIdcardReq);
		saveIdcardCall.callService();
		
		HashMap<String, Long> map = new HashMap<>();
		
		map.put("userId", saveIdcardReq.getUserId());
		map.put("idcardId", saveIdcardCall.getIdcardId());

		return map;
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdatePushStatus_001() {
		Logger.start(true, "更新pushStatus为true");
		
		try {
			
			HashMap<String, Long> map = this.addIdcard(false);
			
			updatePushStatusReq.setIdcardId(map.get("idcardId"));
			updatePushStatusReq.setUserId(map.get("userId"));
			updatePushStatusReq.setPushStatus(true);
			
			updatePushStatusCall.setData(updatePushStatusReq);
			updatePushStatusCall.callService();
			
			Logger.verifyEquals(true, updatePushStatusCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdatePushStatus_002() {
		Logger.start(true, "更新pushStatus为false");
		
		try {
			
			HashMap<String, Long> map = this.addIdcard(true);
			
			updatePushStatusReq.setIdcardId(map.get("idcardId"));
			updatePushStatusReq.setUserId(map.get("userId"));
			updatePushStatusReq.setPushStatus(false);
			
			updatePushStatusCall.setData(updatePushStatusReq);
			updatePushStatusCall.callService();
			
			Logger.verifyEquals(true, updatePushStatusCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdatePushStatus_003() {
		Logger.start(true, "iddcardId和userId不匹配，更新失败");
		
		try {
			
			HashMap<String, Long> map = this.addIdcard(false);
			
			updatePushStatusReq.setIdcardId(map.get("idcardId"));
			updatePushStatusReq.setUserId(1L);
			updatePushStatusReq.setPushStatus(true);
			
			updatePushStatusCall.setData(updatePushStatusReq);
			updatePushStatusCall.callService();
			
			Logger.verifyEquals(false, updatePushStatusCall.getSuccess(), "验证success");
			Logger.verifyEquals(198, updatePushStatusCall.getBCode(), "验证errorCode");
			Logger.verifyEquals("身份证信息不存在", updatePushStatusCall.getMsg(), "验证errorMessage");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdatePushStatus_004() {
		Logger.start(true, "不输入idcardId，更新失败");
		
		try {
			
			updatePushStatusReq.setIdcardId(null);
			updatePushStatusReq.setUserId(1L);
			updatePushStatusReq.setPushStatus(true);
			
			updatePushStatusCall.setData(updatePushStatusReq);
			updatePushStatusCall.callService();
			
			Logger.verifyEquals(false, updatePushStatusCall.getSuccess(), "验证success");
			Logger.verifyEquals(100, updatePushStatusCall.getBCode(), "验证errorCode");
			Logger.verifyEquals("idcardId:must be greater than or equal to 1", updatePushStatusCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdatePushStatus_005() {
		Logger.start(true, "不输入userId，更新失败");
		
		try {
			
			updatePushStatusReq.setIdcardId(1L);
			updatePushStatusReq.setUserId(null);
			updatePushStatusReq.setPushStatus(true);
			
			updatePushStatusCall.setData(updatePushStatusReq);
			updatePushStatusCall.callService();
			
			Logger.verifyEquals(false, updatePushStatusCall.getSuccess(), "验证success");
			Logger.verifyEquals(100, updatePushStatusCall.getBCode(), "验证errorCode");
			Logger.verifyEquals("userId:must be greater than or equal to 1", updatePushStatusCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdatePushStatus_006() {
		Logger.start(true, "不输入pushStatus，更新失败");
		
		try {
			
			updatePushStatusReq.setIdcardId(1L);
			updatePushStatusReq.setUserId(1L);
			updatePushStatusReq.setPushStatus(null);
			
			updatePushStatusCall.setData(updatePushStatusReq);
			updatePushStatusCall.callService();
			
			Logger.verifyEquals(false, updatePushStatusCall.getSuccess(), "验证success");
			Logger.verifyEquals(100, updatePushStatusCall.getBCode(), "验证errorCode");
			Logger.verifyEquals("pushStatus:may not be null", updatePushStatusCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
